# -*- coding:utf-8 -*-
from datetime import datetime
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
from appUtils import WaitSecond, browser_changeTopWindow, browser_doWait, waitInfiniti, getWebDriver, logProxyContent

LoginName = "hiiri2018@163.com"
LoginPd = "Hrg123456"


def loadData(webProxyDriver):
    (browser, _, _) = webProxyDriver
    result = [];
    # 登录
    url = "http://i.360.cn/login/?src=pcw_open_app&destUrl=http%3A%2F%2Fdev.360.cn%3A80%2Fmod3%2Fmobilenavs%2Findex%3F_%3D3042538675"
    browser.get(url)
    browser_doWait(browser)
    # 填充用户名密码
    browser.find_element_by_css_selector("input[name='userName']").send_keys(LoginName)
    browser.find_element_by_css_selector("input[name='password']").send_keys(LoginPd)
    browser.find_element_by_css_selector("input[type='submit']").click()
    browser_doWait(browser)
    browser_changeTopWindow(browser)
    # WebDriverWait.until(EC.presence_of_element_located((By.CLASS_NAME,"guide-btn")))
    # 跳过引导页 会有验证码
    try:
        browser.find_element_by_id("guide1").click()
        browser.find_element_by_id("guide2").click()
        browser.find_element_by_id("guide3").click()
        browser.find_element_by_id("guide4").click()
        browser.find_element_by_id("guide5").click()
        browser.find_element_by_id("guide6").click()
    except Exception as e:
        browser.get("http://dev.360.cn/mod3/mobilenavs/index?_=3042538675&v=2")
        browser.get("http://dev.360.cn/mod3/mobilenavs/index?_=3042538675&v=3")
    browser_doWait(browser)
    browser_changeTopWindow(browser)
    # 解析数据
    iframe = browser.find_element_by_id("idxiframe")
    browser.switch_to.frame(iframe)
    svg = browser.find_element_by_id("highcharts-0")
    pathRoot = svg.find_element_by_css_selector("g[class='highcharts-markers highcharts-tracker']")
    paths = pathRoot.find_elements_by_tag_name("path")
    for item in paths:
        # 鼠标悬停事件
        ActionChains(browser).move_to_element(item).perform()
        tip = svg.find_element_by_css_selector("g[class='highcharts-tooltip']")
        targetText = tip.find_element_by_tag_name("text").text;
        # 01-17今日下载总数:0今日下载趋势:0
        # print(targetText)
        if (len(targetText) > 0):
            _date = targetText[:5]
            findStart = targetText.rfind("今日下载总数") + 7
            findEnd = targetText.find("今日下载趋势")
            _timeStr = targetText[findStart:findEnd]
            result.append((formateDate(_date), _timeStr))
        time.sleep(1)
    # browser.quit()
    print("360",result)
    return result


def formateDate(date):
    """
    01-17转成2018-12-30
    :param date:
    :return:
    """
    _now = datetime.now()
    _nowTuple = _now.timetuple()
    _year = _nowTuple.tm_year
    _dateStr = str(_year) + "-" + date
    _date = datetime.strptime(_dateStr, "%Y-%M-%d")
    _dateTuple = _date.timetuple();
    if (_date > _now):
        _dateStr = str(_year - 1) + "-" + str(_dateTuple.tm_mon) + "-" + str(_dateTuple.tm_mday)
        pass
    return str(_dateTuple.tm_year) + "-" + str(_dateTuple.tm_mon) + "-" + str(_dateTuple.tm_mday)


